package com.futao.service;

import com.futao.entity.User;
import com.futao.sys.model.MyException;
import face.com.futao.dao.IUserDao;
import face.com.futao.service.IUserService;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author futao
 * Created on 18-6-7-上午11:29.
 */
@Transactional
@Service
@Slf4j
public class UserService implements IUserService {

    @Resource
    private GetSession getSession;
    @Resource
    private IUserDao iUserDao;


    @Autowired
    @Qualifier("sessionFactory")
    private SessionFactory sessionFactory;

    /**
     * 添加用户
     *
     * @param user
     * @return
     */
    @Override
    public int add(User user) {
        log.warn(user.toString());
        getSession.getSession().save(user);
        return 1;
    }

    @Override
    public List<User> list() {
        return iUserDao.getUserByUserName(null);
    }

    @Override
    public User get(String id) throws MyException {
        User user = sessionFactory.getCurrentSession().get(User.class, id);
        if (user != null) {
            return user;
        }
        throw new MyException("没有找到数据");
    }

}
